package com.cmcc.cmii.smsmail.listener;

import com.alibaba.fastjson.JSONObject;
import com.cmcc.cmii.smsmail.constants.MailMqConstants;
import com.cmcc.cmii.smsmail.model.constants.MailDataRouterConstants;
import com.cmcc.cmii.smsmail.model.enums.MailTypeIdEnums;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Thanks
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class MailDataRouterListener {


    private final RabbitTemplate rabbitTemplate;



    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MailDataRouterConstants.MAIL_DATA_ROUTER_QUEUE, durable = "true"),
            exchange = @Exchange(value = MailDataRouterConstants.MAIL_DATA_ROUTER_EXCHANGE,
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC, durable = "true"), key = MailDataRouterConstants.MAIL_DATA_ROUTER_KEY))
    public void handle(String jsonStr){
        try {
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);
            String typeId = jsonObject.get("typeId").toString();
            //放入请求队列，记录状态
            rabbitTemplate.convertAndSend(MailMqConstants.MAIL_SEND_STATUS_EXCHANGE,MailMqConstants.MAIL_SEND_STATUS_KEY,jsonStr);
            //普通邮件
            if(MailTypeIdEnums.SIMPLE_MAIL.getTypeId().equalsIgnoreCase(typeId)){
                rabbitTemplate.convertAndSend(MailMqConstants.MAIL_SIMPLE_EXCHANGE,MailMqConstants.MAIL_SIMPLE_KEY,jsonStr);
                return;
            }
            //html邮件
            if(MailTypeIdEnums.HTML_MAIL.getTypeId().equalsIgnoreCase(typeId)){
                rabbitTemplate.convertAndSend(MailMqConstants.MAIL_HTML_EXCHANGE,MailMqConstants.MAIL_HTML_KEY,jsonStr);
                return;
            }

            //附件邮件
            if(MailTypeIdEnums.FILE_MAIL.getTypeId().equalsIgnoreCase(typeId)){
                rabbitTemplate.convertAndSend(MailMqConstants.MAIL_FILE_EXCHANGE,MailMqConstants.MAIL_FILE_KEY,jsonStr);
                return;
            }
            //静态资源
            if(MailTypeIdEnums.RESOURCE_MAIL.getTypeId().equalsIgnoreCase(typeId)){
                rabbitTemplate.convertAndSend(MailMqConstants.MAIL_RESOURCE_EXCHANGE,MailMqConstants.MAIL_RESOURCE_KEY,jsonStr);
                return;
            }
            log.error("**************************发现未匹配类型的消息*******************{}",jsonStr);
        }catch (Exception e){
            e.printStackTrace();
            log.error("===================非邮件类型============={}",jsonStr);
        }
    }

}
